// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    seq: 0,
    seq1: 0,
    sum: 0,
    delivery: 10,
    start: 20,
    name: '',
    num: [],
    result: [],
    menus:[],
   Menu1Item:[
     {path:"",name:"点菜"},
     {path:"",name:"套餐"},
     {path:"",name:"评价"}
    ],
  },
  routerMenu(event){
    this.setData({seq: event.currentTarget.dataset.id});
  },
  selectMenu(event){
    this.setData({seq1: event.currentTarget.dataset.id});
    var index = this.data.seq1;
    this.setData({menus: this.data.result[index]});
  },
  add(event){
    var index = event.currentTarget.dataset.id;
    let num = this.data.num;
    num[index] += 1;
    let sum = parseFloat(this.data.sum),start = parseFloat(this.data.start);
    let price = this.data.menus.foods[index].discount_price;
    sum += parseFloat(price);
    start -= parseFloat(price);
    sum = sum.toFixed(2);
    start = start.toFixed(2);
    console.log(sum);
    console.log(start);
    this.setData({
      num,sum,start
    });
  },
  minus(event){
    var index = event.currentTarget.dataset.id;
    let num = this.data.num;
    num[index] -= 1;
    let sum = parseFloat(this.data.sum), start = parseFloat(this.data.start);
    let price = this.data.menus.foods[index].discount_price;
    sum -= parseFloat(price);
    start += parseFloat(price);
    sum = sum.toFixed(2);
    start = start.toFixed(2);
    this.setData({num, sum, start});
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  initNums(){
    const length = this.data.menus.foods.length;
    let num = this.data.num;
    for(let i = 0; i < length; i++){
      num[i] = 0;
    }
    this.setData({num});
  },
  onLoad(option) {
    this.setData({
      name: option.name,
      result: app.globalData.result,
      menus: app.globalData.result[0]
    })
    this.initNums();
  },
})
